package com.fqcar.fqdf.util;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;

import com.baidu.location.BDLocation;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.utils.route.BaiduMapRoutePlan;
import com.baidu.mapapi.utils.route.RouteParaOption;
import com.fqcar.fqdf.DFApp;
import com.fqcar.fqdf.R;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class NavigationUtils {

    private static final String Baidu = "com.baidu.BaiduMap";
    private static final String Autonavi = "com.autonavi.minimap";

    public static final int Driving = 1;
    public static final int Walking = 2;

    public static final int UseCar = 1;
    public static final int ReturnCar = 2;

//	public String parkingName;

    private static final Map<String, PackageInfo> infoMap = new ConcurrentHashMap<String, PackageInfo>();

    public static void showNavigationDialog(final Context context, final LatLng latLng, final int type, final int useType, final String parkingName) {
        PackageManager pm = context.getPackageManager();
        List<PackageInfo> infos = pm.getInstalledPackages(0);

        for (PackageInfo info : infos) {
//			Log.i(Constants.Log.Log, info.packageName + " " + info.applicationInfo.loadLabel(pm));

            if (info.packageName.equals(Baidu))
                infoMap.put(Baidu, info);
            else if (info.packageName.equals(Autonavi))
                infoMap.put(Autonavi, info);
        }

        String[] titles = new String[]{"高德地图", "百度地图"};

        AlertDialog dialog = new AlertDialog.Builder(context)
                .setSingleChoiceItems(titles, 0, new OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        PackageInfo info = null;
                        String alert = "";
                        String url = "";
                        Intent intent = null;
                        if (which == 0) {
                            info = infoMap.get(Autonavi);
                            alert = context.getResources().getString(R.string.error_not_install_autonavi_map);
//                            LatLng latLng1 = PositionUtil.bd09_To_Gcj02(latLng.latitude,latLng.longitude);

                            intent = new Intent(Intent.ACTION_VIEW);
                            intent.addCategory(Intent.CATEGORY_DEFAULT);
                            intent.setData(Uri.parse("androidamap://route?"
                                    + "sourceApplication=softname"
                                    + "&slat="
                                    + "&slon="
                                    + "&sname=我的位置"
                                    + "&dlat=" + latLng.latitude
                                    + "&dlon=" + latLng.longitude
                                    + "&dname=" + parkingName
//							+ "&dname=" + (useType == UseCar ? "车辆位置" : "还车位置")
                                    + "&dev=0"
                                    + "&did=BGVIS2"
                                    + "&m=2"
                                    + "&t=" + (type == Driving ? "2" : "4")));
                            intent.setPackage(Autonavi);
                            if (info == null) {
                                AlertDialog d = new AlertDialog.Builder(context)
                                        .setTitle(R.string.dialog_title)
                                        .setMessage(alert)
                                        .create();
                                d.show();
                            } else {
                                if (intent != null) {
                                    context.startActivity(intent);
                                }
                            }
                        } else if (which == 1) {
                            BDLocation location = ((DFApp) context.getApplicationContext()).getCurLocation();
//                            BDLocation location = ((GoFunApp) context.getApplicationContext()).getCurBDLocation();

                            LatLng ptStart = new LatLng(location.getLatitude(), location.getLongitude());
                            LatLng storeP = new LatLng(latLng.latitude, latLng.longitude);

                            // 构建 route搜索参数
                            RouteParaOption para = new RouteParaOption()
                                    .startName("我的位置")
                                    .startPoint(ptStart)
                                    .endPoint(storeP)
                                    .endName(parkingName);
                            try {
                                if(type == Driving){
                                    BaiduMapRoutePlan.openBaiduMapDrivingRoute(para, context);
                                }else if(type == Walking){
                                    BaiduMapRoutePlan.openBaiduMapWalkingRoute(para, context);
                                }
                            }catch (Exception e){
                                e.printStackTrace();
                            }
                            /*info = infoMap.get(Baidu);
                            alert = context.getResources().getString(R.string.error_not_install_baidu_map);
                            url = "intent://map/direction?origin=我的位置"
                                    + "&destination=latlng:" + latLng.latitude + "," + latLng.longitude + "|name:" + parkingName
//							+ "&destination=latlng:" + latLng.latitude + "," + latLng.longitude + "|name:" + (useType == UseCar ? "车辆位置" : "还车位置")
                                    + "&mode=" + (type == Driving ? "driving" : "walking")
                                    + "&coord_type=gcj02"
                                    + "&src=" + context.getResources().getString(R.string.app_name) + "#Intent;scheme=bdapp;package=com.baidu.BaiduMap;end";

                            try {
                                intent = Intent.getIntent(url);
                            } catch (URISyntaxException e) {
                                e.printStackTrace();
                            }*/
                        }

                        dialog.dismiss();
						}
                })
                .setPositiveButton(R.string.btn_cancel, new OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                })
                .create();
        dialog.show();
    }
}
